#ifndef FRPGUIMAINWINDOW_H
#define FRPGUIMAINWINDOW_H

#include <QMainWindow>

struct FrpProxy {
    QString name;
    QString type;
    QString localIP;
    QString localPort;
    QString remotePort;
};

namespace Ui {
class FrpGuiMainWindow;
}

class FrpGuiMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit FrpGuiMainWindow(QWidget *parent = nullptr);
    ~FrpGuiMainWindow();

    QString frps_toml_template();
    QString frpc_toml_template();

    QString frps_toml_config();
    void frps_toml_writer(QString);

    QString frpc_toml_config();
    void frpc_toml_writer(QString);

    void statusFrps();
    void statusFrpc();

    void startFrps();
    void startFrpc();

    QString configPath() const;
    QString frpsConfigPath() const;
    QString frpcConfigPath() const;

    QString user_systemd_service_dir() const;
    inline QString user_systemd_service_frps_path() const {
        return user_systemd_service_dir() + "frps.service";
    }
    inline QString user_systemd_service_frpc_path() const {
        return user_systemd_service_dir() + "frpc.service";
    };

    void loadFrpProgramPath();
    void loadFrpProgramConfig();

private slots:
    void choose_frps_tool();
    void choose_frpc_tool();

    void action_frps_text_update();
    void action_frpc_text_update();

    void frps_verify();
    void frpc_verify();

    void frps_status(bool verify);
    void frpc_status(bool verify);

    void frps_apply();
    void frpc_apply();

    void frp_service_alive_check();
    void frps_service_startup(bool alive);
    void frpc_service_startup(bool alive);

    void frp_service_load_check();

    void frps_state_changed(int state);
    void frpc_state_changed(int state);

private:
    bool frp_verify(const QString &program, const QString &config);

    QString frp_systemd_service_unit(const QString &unit, const QString &programPath, const QString &configPath);
    QString frp_systemd_service_unit_template();
    void user_systemd_daemon_reload();
    bool user_systemd_service_show(const QString &service, const QString &attribute, const QString &contains);
    bool user_systemd_service_isActive(const QString &service);
    void user_systemd_service_stop(const QString &service);
    void user_systemd_service_start(const QString &service);
    inline void user_systemd_service_restart(const QString &service) {
        user_systemd_service_stop(service);
        user_systemd_service_start(service);
    };

private:
    Ui::FrpGuiMainWindow *ui;

    QAction *action_frps;
    QAction *action_frpc;

    QString frpsPath;
    QString frpcPath;

    bool user_frps_service_loaded;
    bool user_frpc_service_loaded;
};

#endif // FRPGUIMAINWINDOW_H
